$(function () {
  var retire = false;
  $(".Table>.main .main-left").on("click", "li", function () {
    $(this).addClass("active").siblings().removeClass("active");
    $(".Table .staus .choosed").removeClass('choosed');
  });
  $(".loginOut").off("click").on("click", function () {
    if (retire) {
      App.msg('当前正在进行退菜操作,请先点击"取消"或者"确定"按钮!');
    } else {
      App.confirm("确定退出登录？", function () {
        location.href = "../login.html";
        localStorage.removeItem("curTable");
        localStorage.removeItem("tableInfos");
        localStorage.removeItem("allTables");
        localStorage.removeItem("payCode");
        localStorage.removeItem("category_product");
      })
    }
  });

  $(".zhuotai").off("click").on("click", function () {
    if (retire) {
      App.msg('当前正在进行退菜操作,请先点击"取消"或者"确定"按钮!');
    } else {
      location.href = "Table.html";
    }
  });
  $(".ordermanager").off("click").on("click", function () {
    if (retire) {
      App.msg('当前正在进行退菜操作,请先点击"取消"或者"确定"按钮!');
    } else {
      location.href = "orderForm.html";
    }
  });
  $(".member").off("click").on("click", function () {
    if (retire) {
      App.msg('当前正在进行退菜操作,请先点击"取消"或者"确定"按钮!');
    } else {
      location.href = "memberCenter.html";
    }
  });

  App.ajax("ss_wx/pos_floor", "post", {}, function (res) {
    if (res.flag == true) {
      // localStorage.setItem("allTables", JSON.stringify(res.floor_info));
      if (res.lock_state) {
        $(".btn-jiesuo").removeClass("hidden");
      } else {
        $(".btn-jiesuo").addClass("hidden");
      }
      var floors = [],
        tables = [];
      res.floor_info.forEach(function (v, i) {
        floors.push({
          floor_id: v.floor_id,
          floor_name: v.floor_name
        });
        v.tables.forEach(function (j, k) {
          j.floor_id = v.floor_id;
          if (res.lock_state == false) {
            j.lock_state = false;
          }
        })
        tables.push(v.tables);
      })

      //获取左侧桌台区域
      getFloors(floors);
      // setInterval(function(){
      getAllTableInit(tables);
      // },1000);
      getTables(tables);
    } else {
      App.msg(res.msg)
    }
    if (!res.TokenTrue) {
      App.msg("Token失效，即将跳转到登录页面", function () {
        setTimeout(function () {
          location.href = "../login.html";
        }, 1000)
      })
    }
  }, "", true)
  // }
  var lock = false;
  // var userInfo = JSON.parse(localStorage.getItem("logInfo"));
  var userInfo = JSON.parse(localStorage.getItem("logInfo") || "{}");
  $(".top .userInfo").html(userInfo.login);
  //遍历左侧桌台区域
  function getFloors(floors) {
    //遍历左侧桌台区域
    var floorHtml = '<li class="active" data-floorid="all">全部餐台</li>';
    $.each(floors, function (i, v) {
      floorHtml += '<li data-floorid="' + v.floor_id + '">' + v.floor_name + '</li>';
    });
    $(".Table .main-left").html(floorHtml);
  }

  //初始加载所有桌台
  function getAllTableInit(tables) {
    // var tableInfos = JSON.parse(localStorage.getItem("tableInfos") || "[]");
    // $.ajax({
    //   url:"https://www.easy-mock.com/mock/5b31eba66a4faa2883d772d5/example/getTableState",
    //   type:"post",
    //   dataType:"json",
    //   data:{},
    //   success:function(res){
    var tableHtml = "";
    $.each(tables, function (i, v) {
      $.each(v, function (j, k) {
        // $.each(res,function(a,b){
        //   if(b.table_id==k.table_id){
        //     k.state=b.state;
        //   }
        // })
        // $.each(tableInfos, function (x, y) {
        //   k.state = (k.floor_id == y.floor_id && k.table_id == y.table_id) ? y.state : k.state;
        //   k.has_seat = (k.floor_id == y.floor_id && k.table_id == y.table_id) ? y.has_seat : k.has_seat;
        //   k.t_date = (k.floor_id == y.floor_id && k.table_id == y.table_id) ? y.t_date : k.t_date;
        // })
        tableHtml += ' <li data-tableinfo=\'' + JSON.stringify(k) + '\' class="' + (k.state == 1 ? "white" : k.state == 2 ? "yellow" : k.state == 3 ? "pink" : "green") + '">';
        tableHtml += '<h4>' + k.name + '</h4>';
        tableHtml += '<div class="XDinfo">';
        tableHtml += '<span>' + (k.state == 1 ? "空闲" : k.state == 2 ? "已开台" : k.state == 3 ? "已下单" : "已结账") + '</span>';
        if (k.p_total) {
          tableHtml += '<span>￥ ' + (k.p_total * 1).toFixed(2) + '</span>';
        }
        tableHtml += '</div>';
        tableHtml += '<div class="KT">';
        tableHtml += '<span><span class="has_seat">' + k.has_seat + '</span>/' + k.seats + '</span>';
        tableHtml += '<span>' + k.t_date.substring(11, 16) + '</span>';
        tableHtml += '</div>';
        tableHtml += '<i class="iconfont icon-xuanzhong hidden"></i>';
        tableHtml += '<span class="use_num ' + (k.use_num == 0 ? " hidden" : "") + '">' + k.use_num + '</span>';
        tableHtml += '</li>';
      })
    })
    $(".Table .Tableinfo").html(tableHtml);
    // }
    // })
  }
  //加载桌台区域对应桌台号
  function getTables(tables) {
    // var tableInfos = JSON.parse(localStorage.getItem("tableInfos") || "[]");
    $(".Table .main-left").on("click", "li", function () {
      var floorid = $(this).data("floorid");
      var tableHtml = "";
      $.each(tables, function (i, v) {
        if (floorid == "all") {
          $.each(v, function (j, k) {
            // $.each(tableInfos, function (x, y) {
            //   k.state = (k.floor_id == y.floor_id && k.table_id == y.table_id) ? y.state : k.state;
            //   k.has_seat = (k.floor_id == y.floor_id && k.table_id == y.table_id) ? y.has_seat : k.has_seat;
            //   k.t_date = (k.floor_id == y.floor_id && k.table_id == y.table_id) ? y.t_date : k.t_date;
            // })
            tableHtml += ' <li data-tableinfo=\'' + JSON.stringify(k) + '\' class="' + (k.state == 1 ? "white" : k.state == 2 ? "yellow" : k.state == 3 ? "pink" : "green") + '">';
            tableHtml += '<h4>' + k.name + '</h4>';
            tableHtml += '<div class="XDinfo">';
            tableHtml += '<span>' + (k.state == 1 ? "空闲" : k.state == 2 ? "已开台" : k.state == 3 ? "已下单" : "已结账") + '</span>';
            if (k.p_total) {
              tableHtml += '<span>￥ ' + (k.p_total * 1).toFixed(2) + '</span>';
            }
            tableHtml += '</div>';
            tableHtml += '<div class="KT">';
            tableHtml += '<span><span class="has_seat">' + k.has_seat + '</span>/' + k.seats + '</span>';
            tableHtml += '<span>' + k.t_date.substring(11, 16) + '</span>';
            tableHtml += '</div>';
            tableHtml += '<i class="iconfont icon-xuanzhong hidden"></i>';
            tableHtml += '<span class="use_num ' + (k.use_num == 0 ? " hidden" : "") + '">' + k.use_num + '</span>';
            tableHtml += '</li>';
          })
        } else {
          $.each(v, function (j, k) {
            if (floorid == k.floor_id) {
              // $.each(tableInfos, function (x, y) {
              //   k.state = (k.floor_id == y.floor_id && k.table_id == y.table_id) ? y.state : k.state;
              //   k.has_seat = (k.floor_id == y.floor_id && k.table_id == y.table_id) ? y.has_seat : k.has_seat;
              //   k.t_date = (k.floor_id == y.floor_id && k.table_id == y.table_id) ? y.t_date : k.t_date;
              // })
              tableHtml += ' <li data-tableinfo=\'' + JSON.stringify(k) + '\' class="' + (k.state == 1 ? "white" : k.state == 2 ? "yellow" : k.state == 3 ? "pink" : "green") + '">';
              tableHtml += '<h4>' + k.name + '</h4>';
              tableHtml += '<div class="XDinfo">';
              tableHtml += '<span>' + (k.state == 1 ? "空闲" : k.state == 2 ? "已开台" : k.state == 3 ? "已下单" : "已结账") + '</span>';
              if (k.p_total) {
                tableHtml += '<span>￥ ' + (k.p_total * 1).toFixed(2) + '</span>';
              }
              tableHtml += '</div>';
              tableHtml += '<div class="KT">';
              tableHtml += '<span><span class="has_seat">' + k.has_seat + '</span>/' + k.seats + '</span>';
              tableHtml += '<span>' + k.t_date.substring(11, 16) + '</span>';
              tableHtml += '</div>';
              tableHtml += '<i class="iconfont icon-xuanzhong hidden"></i>';
              tableHtml += '<span class="use_num ' + (k.use_num == 0 ? " hidden" : "") + '">' + k.use_num + '</span>';
              tableHtml += '</li>';
            }
          })
        }
      })
      $(".Table .Tableinfo").html(tableHtml);
    })
  }

  $(".TXTSZ").off("click").on("click", function () {
    console.log("点击系统设置");
    window.samfeng.printPosSET("点击系统设置");
    // window.samfeng.WechatPay(JSON.stringify(wx_pos_order));
    // javascript:samfeng.PSetting();
    /*
    setTimeout(function () {
      location.reload();
    }, 1000)
    */
  })

  //点击桌台
  $(".Table .main-content .Tableinfo").off("click").on("click", "li", function () {
    var _that = this;
    if (retire) {
      App.msg('当前正在进行退菜操作,请先点击"取消"或者"确定"按钮!');
    } else {
      $(".XDBtns .btn").removeAttr("disabled", "disabled");
      var table_info = $(this).data("tableinfo");
      table_info.hasOrder = 1;
      if (table_info.state == 1) {
        $("#PeopleModal .numInput").html("请输入用餐人数");
        $("#PeopleModal").modal("show");
        editPeopleNum("");
        $("#PeopleModal .btnSure").off("click").on("click", function () {
          var peopleNum = $("#PeopleModal .numInput").html();
          if (peopleNum * 1 && peopleNum <= table_info.seats) {
            App.ajax("ss_wx/change_table_seat", "post", {
              table_id: table_info.table_id,
              has_seat: peopleNum
            }, function (res3) {
              if (res3.flag == true) {
                $(this).attr("data-state", "2");
                $(this).removeClass("white").addClass("yellow");
                var tableInfos1 = JSON.parse(localStorage.getItem("tableInfos") || "[]");
                table_info.state = "2";
                table_info.has_seat = peopleNum;
                if (tableInfos1.length > 0) {
                  var equal1 = true;
                  tableInfos1.forEach(function (a, b) {
                    if (a.table_id == table_info.table_id) {
                      if (a.floor_id == table_info.floor_id) {
                        equal1 = false;
                        a.state = "2";
                        a.has_seat = peopleNum;
                        a.t_date = table_info.t_date;
                        a.hasOrder = table_info.hasOrder;
                      }
                    }
                  })
                  if (equal1) {
                    tableInfos1.push(table_info);
                  }
                } else {
                  tableInfos1.push(table_info);
                }
                localStorage.setItem("curTable", JSON.stringify(table_info));
                localStorage.setItem("tableInfos", JSON.stringify(tableInfos1));
                App.ajax("ss_wx/open_table", "post", {
                  table_id: table_info.table_id,
                  table_state: 1,
                }, function (res1) {
                  if (res1.flag == true) {
                    if ($(".btn-jiesuo").hasClass("hidden")) {
                      res1.lock_state = false;
                    }
                    if (res1.lock_state == false) {
                      location.href = "Order.html";
                    } else {
                      if (lock) {
                        $("#JSModal").modal("show");
                        $("#JSModal .btnSure").off("click").on("click", function () {
                          var JSMM = $("#JieSuo").val();
                          if (JSMM == "") {
                            App.msg("请输入密码");
                          } else {
                            App.ajax("ss_wx/check_unlock", "post", {
                              "table_id": table_info.table_id,
                              "order_id": "",
                              "password": JSMM
                            }, function (res) {
                              if (res.flag == true) {
                                App.msg("解锁成功", function () {
                                  setTimeout(function () {
                                    location.reload()
                                  }, 1000)
                                })
                              } else {
                                App.msg(res.msg);
                              }
                            })
                          }
                        })
                        /*  App.ajax("ss_wx/unlock_table", "post", {
                           "table_id": table_info.table_id,
                         }, function (reslock) {
                           if (reslock.flag == true) {
                             layer.open({
                               style: "font-size:16px;",
                               content: "解锁成功",
                               skin: "msg",
                               time: 1,
                               success: function () {
                                 setTimeout(function () {
                                   location.reload()
                                 }, 1000)
                               }
                             })
                           } else {
                             layer.open({
                               style: "font-size:16px;",
                               content: "解锁失败。",
                               skin: "msg",
                               time: 1,
                             })
                           }
                         }) */
                      } else {
                        App.msg("当前桌台处于锁定状态，可点击解锁按钮进行解锁", function () {
                          setTimeout(function () {
                            location.reload();
                          }, 1000)
                        })
                      }
                    }
                  } else {
                    localStorage.removeItem("curTable");
                    /*   layer.open({
                        style: "font-size:16px;",
                        content: res1.msg ? res1.msg : "桌台状态已改变，即将刷新桌台状态。",
                        skin: "msg",
                        time: 1,
                        success: function () {
                          setTimeout(function () {
                            location.reload()
                          }, 1000)
                        }
                      }); */
                    App.msg(res1.msg ? res1.msg : "桌台状态已改变，即将刷新桌台状态。", function () {
                      setTimeout(function () {
                        location.reload();
                        localStorage.removeItem("tableInfos");
                      }, 1000);
                    })
                  }
                  if (!res1.TokenTrue) {
                    App.msg("Token失效，即将跳转到登录页面", function () {
                      setTimeout(function () {
                        location.href = "../login.html";
                      }, 1000)
                    })
                  }
                })
                $("#PeopleModal").modal("hide");
                // location.href = "Order.html";
              } else {
                App.msg("输入用餐人数失败。")
              }
              if (!res3.TokenTrue) {
                App.msg("Token失效，即将跳转到登录页面", function () {
                  setTimeout(function () {
                    location.href = "../login.html";
                  }, 1000)
                })
              }
            }, "", true)
          } else {
            App.msg("请输入用餐人数,不要超过当前桌座位数。");
          }
        })
      } else if (table_info.state == 2) {
        if ($(_that).find(".icon-xuanzhong").hasClass("hidden")) {
          App.ajax("ss_wx/open_table", "post", {
            table_id: table_info.table_id,
            table_state: 2
          }, function (res1) {
            if (res1.flag == true) {
              if ($(".btn-jiesuo").hasClass("hidden")) {
                res1.lock_state = false;
              }
              if (res1.lock_state == false) {
                $(_that).find(".icon-xuanzhong").removeClass("hidden")
                $(_that).siblings().find(".icon-xuanzhong").addClass("hidden");
                $(".Table .main-right .noData").addClass("hidden");
                $(".Table .main-right .hasData").removeClass("hidden");
                $(".Table .main-right_top .peopleNum").removeClass("hidden");
                $("#editPeople").addClass("hidden");
                $(".Table .main-right .peopleNum .tableNam").html(table_info.name);
                $(".Table .main-right .peopleNum .num").html(table_info.has_seat);
                $(".Table .controlGroup .XDBtns,.Table .controlGroup .JZBtns").addClass("hidden");
                $(".Table .controlGroup .KTBtns").removeClass("hidden");
                $(".Table .main-right .money").addClass("hidden");
                $(".Table .dishList_YD").empty();
                var tableInfos2 = JSON.parse(localStorage.getItem("tableInfos") || "[]");
                if (tableInfos2.length > 0) {
                  var equal2 = true;
                  tableInfos2.forEach(function (a, b) {
                    if (a.table_id == table_info.table_id) {
                      if (a.floor_id == table_info.floor_id) {
                        equal2 = false;
                        table_info.ZDBZ = a.ZDBZ;
                        table_info.has_seat = a.has_seat;
                        table_info.cart = a.cart;
                        table_info.totalInfo = a.totalInfo;
                      }
                    }
                  })
                  if (equal2) {
                    tableInfos2.push(table_info);
                  }
                } else {
                  tableInfos2.push(table_info);
                }
                localStorage.setItem("curTable", JSON.stringify(table_info));
                localStorage.setItem("tableInfos", JSON.stringify(tableInfos2));
                //已开台清台
                $("#clearKT").off("click").on("click", function () {
                  App.ajax("ss_wx/open_table", "post", {
                    table_id: table_info.table_id,
                    table_state: 2
                  }, function (res_1) {
                    if (res_1.flag == true) {
                      if ($(".btn-jiesuo").hasClass("hidden")) {
                        res_1.lock_state = false;
                      }
                      if (res_1.lock_state == false) {
                        App.confirm("确定清台？", function () {
                          App.ajax("ss_wx/clean_table", "post", {
                            table_id: table_info.table_id
                          }, function (res2) {
                            if (res2.flag == true) {
                              localStorage.removeItem("curTable");
                              var tableInfos = JSON.parse(localStorage.getItem("tableInfos") || "[]");
                              tableInfos.forEach(function (k, j) {
                                if (k.table_id == table_info.table_id) {
                                  tableInfos.splice(j, 1);
                                }
                              })
                              localStorage.setItem("tableInfos", JSON.stringify(tableInfos));
                              location.reload();
                            } else {
                              App.confirm(res2.msg + '[或者点击确定可以进行订单删除,并强制清台!]', function () {
                                App.ajax("ss_wx/clean_table", "post", {
                                  'table_id': table_info.table_id,
                                  'forceDelete': true
                                }, function (res) {
                                  if (res.flag == true) {
                                    App.msg("清台成功!");
                                    location.reload();
                                  } else {
                                    App.msg(res.msg);
                                  }
                                  if (!res.TokenTrue) {
                                    App.msg("Token失效，即将跳转到登录页面", function () {
                                      setTimeout(function () {
                                        location.href = "../login.html";
                                      }, 1000)
                                    })
                                  }
                                })
                              });
                            }
                            if (!res2.TokenTrue) {
                              App.msg("Token失效，即将跳转到登录页面", function () {
                                setTimeout(function () {
                                  location.href = "../login.html";
                                }, 1000)
                              })
                            }
                          })
                        })
                      } else {
                        App.msg("当前桌台已锁定，请稍后操作", function () {
                          setTimeout(function () {
                            location.reload();
                          }, 1000)
                        });
                      }
                    } else {
                      localStorage.removeItem("curTable");
                      App.msg(res1.msg ? res1.msg : "桌台状态已改变，即将刷新桌台状态。", function () {
                        setTimeout(function () {
                          location.reload();
                          localStorage.removeItem("tableInfos");
                        }, 1000)
                      })
                    }
                  })

                })


                //已开台点菜按钮

                $("#addKT").off("click").on("click", function () {
                  App.ajax("ss_wx/open_table", "post", {
                    table_id: table_info.table_id,
                    table_state: 2,
                  }, function (res_1) {
                    if (res_1.flag == true) {
                      if ($(".btn-jiesuo").hasClass("hidden")) {
                        res_1.lock_state = false;
                      }
                      if (res_1.lock_state == false) {
                        App.ajax("ss_wx/lock_table", "post", {
                          "table_id": table_info.table_id
                        }, function (res5) {
                          if (res5.flag == true) {
                            location.href = "Order.html";
                          } else {
                            App.msg("锁定桌台失败");
                          }
                          if (!res5.TokenTrue) {
                            App.msg("Token失效，即将跳转到登录页面", function () {
                              setTimeout(function () {
                                location.href = "../login.html";
                              }, 1000)
                            })
                          }
                        })
                      } else {
                        App.msg("当前桌台已锁定，请稍后操作", function () {
                          setTimeout(function () {
                            location.reload();
                          }, 1000)
                        });
                      }
                    } else {
                      localStorage.removeItem("curTable");
                      App.msg(res1.msg ? res1.msg : "桌台状态已改变，即将刷新桌台状态。", function () {
                        setTimeout(function () {
                          location.reload();
                          localStorage.removeItem("tableInfos");
                        }, 1000)
                      })
                    }
                  })

                })


              } else {
                if (lock) {
                  $("#JSModal").modal("show");
                  $("#JSModal .btnSure").off("click").on("click", function () {
                    var JSMM = $("#JieSuo").val();
                    if (JSMM == "") {
                      App.msg("请输入密码");
                    } else {
                      App.ajax("ss_wx/check_unlock", "post", {
                        "table_id": table_info.table_id,
                        "order_id": table_info.order_id,
                        "password": JSMM
                      }, function (res) {
                        if (res.flag == true) {
                          App.msg("解锁成功", function () {
                            setTimeout(function () {
                              location.reload()
                            }, 1000)
                          })
                        } else {
                          App.msg(res.msg);
                        }
                        if (!res.TokenTrue) {
                          App.msg("Token失效，即将跳转到登录页面", function () {
                            setTimeout(function () {
                              location.href = "../login.html";
                            }, 1000)
                          })
                        }
                      })
                    }
                  })
                  /*  App.ajax("ss_wx/unlock_table", "post", {
                     "table_id": table_info.table_id,
                   }, function (reslock) {
                     if (reslock.flag == true) {
                       layer.open({
                         style: "font-size:16px;",
                         content: "解锁成功",
                         skin: "msg",
                         time: 1,
                         success: function () {
                           setTimeout(function () {
                             location.reload()
                           }, 1000)
                         }
                       })
                     } else {
                       layer.open({
                         style: "font-size:16px;",
                         content: "解锁失败。",
                         skin: "msg",
                         time: 1,
                       })
                     }
                   }) */
                } else {
                  App.msg("当前桌台处于锁定状态，可点击解锁按钮进行解锁", function () {
                    setTimeout(function () {
                      location.reload();
                    }, 1000)
                  })
                }
              }
            } else {
              localStorage.removeItem("curTable");
              App.msg(res1.msg ? res1.msg : "桌台状态已改变，即将刷新桌台状态。", function () {
                setTimeout(function () {
                  location.reload();
                  localStorage.removeItem("tableInfos");
                }, 1000);
              })
            }
            if (!res1.TokenTrue) {
              App.msg("Token失效，即将跳转到登录页面", function () {
                setTimeout(function () {
                  location.href = "../login.html";
                }, 1000)
              })
            }
          }, "", true)

        } else {
          $(_that).find(".icon-xuanzhong").addClass("hidden");
          $(".Table .main-right .noData").removeClass("hidden");
          $(".Table .main-right .hasData").addClass("hidden");
          $(".Table .main-right_top .peopleNum").addClass("hidden");
          $("#editPeople").addClass("hidden");
        }
      } else {
        if ($(_that).find(".icon-xuanzhong").hasClass("hidden")) {
          App.ajax("ss_wx/open_table", "post", {
            table_id: table_info.table_id,
            table_state: 3,
            order_id: table_info.order_id,
            order_timestamp: table_info.order_timestamp,
          }, function (res1) {
            if (res1.flag == true) {
              if ($(".btn-jiesuo").hasClass("hidden")) {
                res1.lock_state = false;
              }
              if (res1.lock_state == false) {
                if (res1.update_timestamp == false) {
                  // $(".Table .main-right_top .peopleNum,.Table .main-right_top .btn").removeClass("hidden");
                  $(_that).find(".icon-xuanzhong").removeClass("hidden")
                  $(_that).siblings().find(".icon-xuanzhong").addClass("hidden");
                  $(".Table .main-right .noData").addClass("hidden");
                  $(".Table .main-right .hasData").removeClass("hidden");
                  $(".Table .main-right_top .peopleNum").removeClass("hidden");
                  $("#editPeople").removeClass("hidden");

                  $(".Table .main-right .peopleNum .tableNam").html(table_info.name);
                  $(".Table .main-right .peopleNum .num").html(table_info.has_seat);
                  $(".Table .controlGroup .XDBtns").removeClass("hidden");
                  $(".Table .controlGroup .JZBtns,.Table .controlGroup .KTBtns").addClass("hidden");
                  $(".Table .main-right .money .stateIcon").html("<i class='iconfont icon-yixiadan'></i>");

                  var tableInfos3 = JSON.parse(localStorage.getItem("tableInfos") || "[]");
                  if (tableInfos3.length > 0) {
                    var orderHtml3 = "";
                    var equal3 = true;
                    if (equal3) {
                      getserverData(tableInfos3, table_info);
                    }
                  } else {
                    getserverData(tableInfos3, table_info);
                  }
                  //点击修改人数
                  $("#editPeople").off("click").on("click", function () {
                    var that_ = this;
                    App.ajax("ss_wx/open_table", "post", {
                      table_id: table_info.table_id,
                      table_state: 3,
                    }, function (res_1) {
                      if (res_1.flag == true) {
                        if ($(".btn-jiesuo").hasClass("hidden")) {
                          res_1.lock_state = false;
                        }
                        if (res_1.lock_state == false) {
                          var that = this;
                          var peoNum = table_info.has_seat;
                          $("#PeopleModal").modal("show");
                          editPeopleNum(peoNum);

                          $("#PeopleModal .numInput").html(peoNum);
                          //点击确认
                          $("#PeopleModal .btnSure").off("click").on("click", function () {
                            var newNum = $("#PeopleModal .numInput").html();
                            if (peoNum == newNum) {
                              $("#PeopleModal").modal("hide");
                            } else if (newNum > table_info.seats) {
                              App.msg("超出本桌台人数");
                            } else if (!(newNum * 1)) {
                              App.msg("输入用餐人数");

                            } else {
                              newNum = newNum * 1;
                              App.ajax("ss_wx/change_table_seat", "post", {
                                table_id: table_info.table_id,
                                has_seat: newNum
                              }, function (res3) {
                                if (res3.flag == true) {
                                  App.msg("修改人数成功");
                                  $(_that).find(".has_seat").html(newNum);
                                  $(that_).prev("").find(".num").html(newNum);
                                  table_info.has_seat = newNum;
                                  localStorage.setItem("curTable", JSON.stringify(table_info));
                                  $.each(tableInfos3, function (j, k) {
                                    if (k.floor_id == table_info.floor_id && k.table_id == table_info.table_id) {
                                      k.has_seat = newNum;
                                    }
                                  })
                                  localStorage.setItem("tableInfos", JSON.stringify(tableInfos3));
                                  $("#PeopleModal").modal("hide");
                                }
                                if (!res3.TokenTrue) {
                                  App.msg("Token失效，即将跳转到登录页面", function () {
                                    setTimeout(function () {
                                      location.href = "../login.html";
                                    }, 1000)
                                  })
                                }
                              })
                            }
                          })
                        } else {
                          App.msg("当前桌台已锁定，请稍后操作", function () {
                            setTimeout(function () {
                              location.reload();
                            }, 1000)
                          });
                        }
                      } else {
                        localStorage.removeItem("curTable");
                        App.msg(res1.msg ? res1.msg : "桌台状态已改变，即将刷新桌台状态。", function () {
                          setTimeout(function () {
                            location.reload();
                            localStorage.removeItem("tableInfos");
                          }, 1000)
                        })
                      }
                      if (!res_1.TokenTrue) {
                        App.msg("Token失效，即将跳转到登录页面", function () {
                          setTimeout(function () {
                            location.href = "../login.html";
                          }, 1000)
                        })
                      }
                    })
                  });
                  //点击退菜
                  setTimeout(function () {
                    var initData = JSON.parse(localStorage.getItem("curTable"));
                    Retire(tableInfos3, table_info, initData);
                  }, 300);
                  //点击退单
                  TuiDan(table_info);
                  //点击打印
                  print(table_info);
                  //移合台
                  TransTable(table_info);
                  //点击结账
                  $(".checkOut").off("click").on("click", function () {
                    App.ajax("ss_wx/open_table", "post", {
                      table_id: table_info.table_id,
                      table_state: 3,
                      order_id: table_info.order_id,
                      order_timestamp: table_info.order_timestamp,
                    }, function (res_1) {
                      if (res_1.flag == true) {
                        if ($(".btn-jiesuo").hasClass("hidden")) {
                          res_1.lock_state = false;
                        }
                        if (res_1.lock_state == false) {
                          if (res_1.update_timestamp == false) {
                            location.href = "../pages/Cashier_1.html";
                          } else {
                            App.msg(res_1.msg, function () {
                              setTimeout(function () {
                                location.reload();
                                localStorage.removeItem("tableInfos");
                              }, 1000)
                            })
                          }
                        } else {
                          App.msg("当前桌台已锁定，请稍后操作", function () {
                            setTimeout(function () {
                              location.reload();
                            }, 1000)
                          });
                        }
                      } else {
                        localStorage.removeItem("curTable");
                        App.msg(res1.msg ? res1.msg : "桌台状态已改变，即将刷新桌台状态。", function () {
                          setTimeout(function () {
                            location.reload();
                            localStorage.removeItem("tableInfos");
                          }, 1000)
                        })
                      }
                      if (!res_1.TokenTrue) {
                        App.msg("Token失效，即将跳转到登录页面", function () {
                          setTimeout(function () {
                            location.href = "../login.html";
                          }, 1000)
                        })
                      }
                    })
                  })
                } else {
                  App.msg(res1.msg, function () {
                    setTimeout(function () {
                      location.reload();
                      localStorage.removeItem("tableInfos")
                    }, 1000)
                  })
                }
              } else {
                App.msg("当前桌台处于锁定状态，请等待其他人操作完成后在尝试", function () {
                  setTimeout(function () {
                    location.reload();
                  }, 1000)
                });
              }
            } else {
              localStorage.removeItem("curTable");
              App.msg(res1.msg ? res1.msg : "桌台状态已改变，即将刷新桌台状态。", function () {
                setTimeout(function () {
                  location.reload();
                  localStorage.removeItem("tableInfos");
                }, 1000)
              })
            }
            if (!res1.TokenTrue) {
              App.msg("Token失效，即将跳转到登录页面", function () {
                setTimeout(function () {
                  location.href = "../login.html";
                }, 1000)
              })
            }
          }, "", true)
        } else {
          $(_that).find(".icon-xuanzhong").addClass("hidden");
          $(".Table .main-right .noData").removeClass("hidden");
          $(".Table .main-right .hasData").addClass("hidden");
          $(".Table .main-right_top .peopleNum").addClass("hidden");
          $("#editPeople").addClass("hidden");
        }
      }
    }
  });

  //不同桌台状态处理的事件


  //点击桌台桌台状态为3，4如果没有缓存，拉取服务器数据
  function getserverData(tableInfos, table_info) {
    App.ajax("ss_wx/table_order_details", "post", {
      table_id: table_info.table_id
    }, function (res) {
      if (res.flag == true) {
        var orderHtml = "";
        table_info.m_date = res.order_detail.m_date;
        table_info.f_date = res.order_detail.f_date;
        table_info.order_id = res.order_detail.order_id;
        table_info.origin_reference = res.order_detail.origin_reference;
        table_info.order_sequence = res.order_detail.order_sequence;
        var totalnum = 0;
        res.order_detail.pay_info.forEach(function (a, b) {
          totalnum += a.num;
        })
        table_info.totalInfo = {
          totalCount: totalnum,
          totalMon: res.order_detail.p_total
        };
        res.order_detail.pay_info.forEach(function (x) {
          x.rsta = 1;
          x.ZFname = "";
          x.ZFprice = 0;
          if (x.cuisine && x.cuisine.length > 0) {
            x.cuisine.forEach(function (b, a) {
              x.ZFname += b.name + " ";
              x.ZFprice += b.price * 1;
            })
          }
          // x.ZFname = x.cuisine && x.cuisine.length > 0 ? x.cuisine[0].name : "";
          // x.ZFid = x.cuisine && x.cuisine.length > 0 ? x.cuisine[0].id : "";
          // x.ZFprice = x.cuisine && x.cuisine.length > 0 ? x.cuisine[0].price : "";
          if (x.combo_details && x.combo_details.length > 0) {
            var tcprice = 0;
            x.combo_details.forEach(function (m, n) {
              m.menu_list.forEach(function (a, b) {
                tcprice += (a.price * a.select_num);
                if (m.category_leixing == "客人自选") {
                  a.choosedNum = a.select_num
                }
              })
            });
            if (x.extend_combo_flag) {
              x.totalTCPrice = x.price;
            } else {
              // x.price = tcprice;
              x.totalTCPrice = tcprice;
            }
          }
          x.combo_details = x.combo_details && x.combo_details.length > 0 ? x.combo_details : "";
          x.combo_details1 = x.combo_details && x.combo_details.length > 0 ? x.combo_details : "";
          orderHtml += "<li class='" + (x.num == 0 ? "red" : "") + "' data-TCInfos='" + JSON.stringify(x) + "'>" +
              "<div class='pack hidden'>打包</div><div class='waitCall hidden'>等叫</div><div class='dishName'>" +
              x.name + (x.ZFname ? "<span style='color:#f40'> | " + x.ZFname + "</span>" :
                  (x.combo_details && x.combo_details.length > 0 ? "" +
                      "<a style='margin-left:8px;' class='btn btn-sm btn-warning btn-details' data-toggle='popover' >" +
                      "套餐详情</a>" : "")) + "</div><div class='control'> " +
              "<i data-control='minus' class='iconfont icon-minus-circle hidden'></i><span class='dishNum'>" + x.num +
              "</span> <i data-control='plus' class='iconfont icon-plus-circle hidden'></i> " +
              "</div><div class='dishPrice'>" + parseFloat(x.price * x.num).toFixed(2) + "</div></li>"

          // orderHtml += "<li><span>" + x.name + "</span>" +
          //   "<span>" + x.num + "</span>" +
          //   "<span>" + (x.price*x.num) + "</span>" +
          //   "</li>";
        })
        $(".Table .dishList_YD").html(orderHtml);
        let orderInfo = res.order_detail.pay_info;
        table_info.cart = orderInfo;
        $(".Table .main-right .dishToal").html(totalnum);
        $(".Table .main-right .dishMoney").html(res.order_detail.p_total);
        localStorage.setItem("curTable", JSON.stringify(table_info));
        tableInfos.push(table_info);
        localStorage.setItem("tableInfos", JSON.stringify(tableInfos));
        let showInfo = {
          "pay_info": orderInfo,
          "p_total": res.order_detail.p_total
        };
        window.samfeng.showInfo(JSON.stringify(showInfo));
      } else {
        App.msg(res.msg);
      }
      if (!res.TokenTrue) {
        App.msg("Token失效，即将跳转到登录页面", function () {
          setTimeout(function () {
            location.href = "../login.html";
          }, 1000)
        })
      }
    }, "", true)
  }

  //点击套餐详情
  // mouseover
  $(".Table .dishList_YD ").off("mouseover").on("mouseover", ".btn-details", function () {
    var dishes = $(this).parents("li").data("tcinfos");
    var content = "";
    var menu_list = [];
    dishes.combo_details1.forEach(function (x, y) {
      x.menu_list.forEach(function (a, b) {
        menu_list.push(a);
      })
    })
    menu_list.forEach(function (a, b) {
      content += b + 1 + "." + a.name + '---' + (a.choosedNum ? a.choosedNum : a.select_num) + a.uom + '---' + a.price + '元/' + a.uom + "<br/>"
    })
    $(this).popover({
      content: content,
      placement: "left",
      trigger: "click",
      container: 'body',
      html: true
    })
  });

  //点击桌台不同状态
  $(".Table .staus").off("click").on("click", "button", function () {
    $(this).addClass("choosed").siblings().removeClass("choosed");
    var status = $(this).data("state");
    $(".Table .Tableinfo li").each(function (i, v) {
      var tableinfo = JSON.parse($(this).attr("data-tableinfo"));
      if (tableinfo.state == status) {
        $(this).removeClass("hidden");
      } else {
        $(this).addClass("hidden");
      }
    })
  })

  //修改人数
  function editPeopleNum(peopleNum) {
    $("#PeopleModal .numbox .item div").off("click").on("click", function () {
      var num = $(this).data("num");
      var control = $(this).data("control");
      if (num || num == 0) {
        peopleNum += num + "";
        $(".numInput").html(peopleNum);
      }
      if (control) {
        $(".numInput").html("输入用餐人数");
        peopleNum = "";
      }
    })
  }

  //点击移/合台
  function TransTable(table_info) {
    $(".TransTable").off("click").on("click", function () {
      $(".toright").addClass('hidden');
      $(".toleft").addClass('hidden');
      var select_table = false;
      App.ajax("ss_wx/open_table", "post", {
        table_id: table_info.table_id,
        table_state: 3,
        order_id: table_info.order_id,
        order_timestamp: table_info.order_timestamp,
      }, function (res_1) {
        if (res_1.flag == true) {
          if ($(".btn-jiesuo").hasClass("hidden")) {
            res_1.lock_state = false;
          }
          if (res_1.lock_state == false) {
            if (res_1.update_timestamp == false) {
              $("#YHTModal").modal("show");
              var curTable = JSON.parse(localStorage.getItem("curTable"));
              curTable.origin_reference = res_1.origin_reference;
              var tableInfos = JSON.parse(localStorage.getItem("tableInfos"));
              var YHTable = [];
              $("#YHTModal .transfer .right .tit span").html("");
              $("#YHTModal .right ul").html("");
              // 获取桌台信息
              pos_floor();

              function pos_floor() {
                App.ajax("ss_wx/pos_floor", "post", {}, function (res) {
                  if (res.flag == true) {

                    var selectHtml = "";
                    res.floor_info.forEach(function (v, i) {
                      selectHtml += "<optgroup label='" + v.floor_name + "'>";
                      v.tables.forEach(function (a, b) {
                        a.floor_id = v.floor_id;
                        if (a.table_id != curTable.table_id && a.state != 4) {
                          selectHtml += "<option value='" + JSON.stringify(a) + "'>" + a.name + "-----(" + (a.state == 1 ? "空闲" : a.state == 2 ? "已开台" : a.state == 3 ? "已下单" : "已结账") + ")" + (a.lock_state && res.lock_state ? '-----已锁定' : '') + "</option>";
                        }
                      })
                      selectHtml += "</optgroup>";
                    })
                    $("#selectTab").html(selectHtml);
                    $('#selectTab').selectpicker('val', "");
                    $('#selectTab').selectpicker('refresh');
                  } else {
                    App.msg(res.msg, function () {
                      setTimeout(function () {
                        location.href = "../login.html";
                      }, 1000)
                    })
                  }
                  if (!res.TokenTrue) {
                    App.msg("Token失效，即将跳转到登录页面", function () {
                      setTimeout(function () {
                        location.href = "../login.html";
                      }, 1000)
                    })
                  }
                })
              }
              //获取当前桌的下单菜品
              getOrderDetails(curTable, "#YHTModal .left ul");

              function getOrderDetails(tableStorage, className) {
                App.ajax("ss_wx/table_order_details", "post", {
                  table_id: tableStorage.table_id
                }, function (res) {
                  if (res.flag == true) {
                    if ($(".btn-jiesuo").hasClass("hidden")) {
                      res.lock_state = false;
                    }
                    if (res.lock_state == false) {
                      var curTabHtml = "<li class='listTit'><span>菜名称</span><span>数量</span><span>价格</span></li>";
                      if (className.indexOf(".right") != -1) {
                        if (res.order_detail) {
                          YHTable = res.order_detail;
                        } else {
                          YHTable = {
                            "order_id": "",
                            "p_total": 0,
                            "pay_info": []
                          }
                        }
                        $("#YHTModal .transfer .right .tit span").html(tableStorage.name);
                      } else {
                        $("#YHTModal .transfer .left .tit span").html(tableStorage.name);
                      }
                      if (res.order_detail) {
                        res.order_detail.pay_info.forEach(function (a, b) {

                          a.rsta = 1;
                          // a.ZFname = a.cuisine && a.cuisine.length > 0 ? a.cuisine[0].name : "";
                          // a.ZFid = a.cuisine && a.cuisine.length > 0 ? a.cuisine[0].id : "";
                          // a.ZFprice = a.cuisine && a.cuisine.length > 0 ? a.cuisine[0].price : "";
                          a.ZFname = "";
                          a.ZFprice = 0;
                          if (a.cuisine) {
                            a.cuisine.forEach(function (y, z) {
                              a.ZFname += y.name + " ";
                              a.ZFprice += y.price * 1;
                            })
                          }
                          if (a.combo_details && a.combo_details.length > 0) {
                            var tcprice = 0;
                            a.combo_details.forEach(function (m, n) {
                              m.menu_list.forEach(function (a, b) {
                                tcprice += (a.price * a.select_num);
                                if (m.category_leixing == "客人自选") {
                                  a.choosedNum = a.select_num
                                }
                              })
                            });
                            if (a.extend_combo_flag) {
                              a.totalTCPrice = a.price
                            } else {
                              a.price = tcprice;
                              a.totalTCPrice = tcprice;
                            }

                          }
                          a.combo_details = a.combo_details && a.combo_details.length > 0 ? a.combo_details : "";
                          a.combo_details1 = a.combo_details && a.combo_details.length > 0 ? a.combo_details : "";


                          curTabHtml += "<li data-dishinfo='" + JSON.stringify(a) + "'>";
                          curTabHtml += "<span>" + a.name + "<span style='color:#f40'>" +
                              (a.ZFname ? " | " + a.ZFname : "") + "</span></span><span>" + a.num + "</span><span>" +
                              parseFloat(a.price * a.num).toFixed(2) + "</span>";
                          curTabHtml += "</li>";
                        })
                      }
                      $(className).html(curTabHtml);
                      if (tableStorage.table_id == curTable.table_id) {
                        App.ajax("ss_wx/lock_table", "post", {
                          "table_id": curTable.table_id
                        }, function (res5) {
                          if (res5.flag == true) {} else {
                            App.msg("锁定桌台失败");
                          }
                        })
                      } else {
                        YHTable.tabinfos = tableStorage;
                      }
                    } else {
                      pos_floor();
                      App.msg(res.table_name + "处于锁定状态，请稍后进行操作");
                    }
                  } else {
                    App.msg(res.msg);
                  }
                  if (!res.TokenTrue) {
                    App.msg("Token失效，即将跳转到登录页面", function () {
                      setTimeout(function () {
                        location.href = "../login.html";
                      }, 1000)
                    })
                  }
                })
              }
              //当下拉框改变时
              $("#selectTab").off("changed.bs.select").on("changed.bs.select", function () {
                var val = $(this).val();
                if (val) {
                  if (!select_table) {
                    select_table = val;
                    val = JSON.parse(val);
                    getOrderDetails(val, "#YHTModal .right ul");
                    $(".toright").removeClass('hidden');
                    $(".toleft").removeClass('hidden');
                  } else {
                    if (select_table != val) {
                      App.confirm("切换桌台,请取消当前移合台操作,再进行点击移合台操作。", function () {
                        App.unlocktable(curTable.table_id);
                        $(".toright").addClass('hidden');
                        $(".toleft").addClass('hidden');
                      }, false, true);
                    } else {
                      val = JSON.parse(val);
                      getOrderDetails(val, "#YHTModal .right ul");
                      $(".toright").removeClass('hidden');
                      $(".toleft").removeClass('hidden');
                    }
                  }
                }
              });
              //点击要移动的菜品
              $("#YHTModal .transfer ").off("click").on("click", "li:not('.listTit')", function () {
                if ($(this).hasClass("choosedDish")) {
                  $(this).removeClass("choosedDish")
                } else {
                  $(this).addClass("choosedDish");
                }
              });
              //点击右箭头
              $("#YHTModal .transfer .toright").off("click").on("click", function () {
                if ($("#selectTab").val() == null) {
                  App.msg("请选择桌台");
                } else {
                  var choosedDish = [];
                  var changedCurTable = [];
                  $("#YHTModal .transfer .left li").each(function (v, k) {
                    var dishinfo = $(this).data("dishinfo");

                    if ($(this).hasClass("choosedDish")) {
                      choosedDish.push(dishinfo);
                      $(this).remove();
                    } else {
                      if (dishinfo) {
                        changedCurTable.push(dishinfo);
                      }
                    }
                  });
                  var choosedDishHtml = "";
                  choosedDish.forEach(function (x, y) {
                    choosedDishHtml += "<li class='choosedDish' data-dishinfo='" + JSON.stringify(x) + "'>";
                    choosedDishHtml += "<span>" + x.name + "</span><span>" + x.num + "</span><span>" + parseFloat(x.price * x.num).toFixed(2) + "</span>";
                    choosedDishHtml += "</li>";
                  })
                  $("#YHTModal .right ul").append(choosedDishHtml);
                  // console.log(choosedDish);
                  var totalCount = 0.0,
                    totalMoney = 0.0;
                  // console.log(changedCurTable);
                  changedCurTable.forEach(function (m, n) {
                    totalCount += m.num * 1;
                    totalMoney += m.num * m.price;
                  })
                  curTable.cart = changedCurTable;
                  curTable.totalInfo.totalCount = totalCount;
                  curTable.totalInfo.totalMon = parseFloat(totalMoney).toFixed(2);
                  YHTable.pay_info = YHTable.pay_info.concat(choosedDish);
                  // console.log(YHTable.pay_info);
                  var p_total = 0;
                  YHTable.pay_info.forEach(function (m, n) {
                    p_total += m.num * m.price;
                  });
                  YHTable.p_total = parseFloat(p_total).toFixed(2);
                }
              })
              //点击左箭头
              $("#YHTModal .transfer .toleft").off("click").on("click", function () {
                if ($("#selectTab").val() == null) {
                  App.msg("请选择桌台")
                } else {
                  var choosedDish = [];
                  var changedCurTable = [];
                  $("#YHTModal .transfer .right li").each(function (v, k) {
                    var dishinfo = $(this).data("dishinfo");
                    if ($(this).hasClass("choosedDish")) {
                      choosedDish.push(dishinfo);
                      $(this).remove();
                    } else {
                      if (dishinfo) {
                        changedCurTable.push(dishinfo);
                      }
                    }
                  })
                  var choosedDishHtml = "";
                  choosedDish.forEach(function (x, y) {
                    choosedDishHtml += "<li class='choosedDish' data-dishinfo='" + JSON.stringify(x) + "'>";
                    choosedDishHtml += "<span>" + x.name + "</span><span>" + x.num + "</span><span>" + parseFloat(x.price * x.num).toFixed(2) + "</span>";
                    choosedDishHtml += "</li>";
                  })
                  $("#YHTModal .left ul").append(choosedDishHtml);
                  // console.log(choosedDish);
                  // console.log(YHTable);
                  YHTable.pay_info = changedCurTable;
                  var p_total = 0.0;
                  changedCurTable.forEach(function (a, b) {
                    p_total += a.num * a.price
                  })
                  YHTable.p_total = parseFloat(p_total).toFixed(2);
                  // console.log(YHTable.pay_info);
                  curTable.cart = curTable.cart.concat(choosedDish);
                  var totalCount = 0.0,
                    totalMoney = 0.0;
                  curTable.cart.forEach(function (m, n) {
                    totalCount += m.num * 1;
                    totalMoney += m.num * m.price
                  })
                  curTable.totalInfo.totalCount = totalCount;
                  curTable.totalInfo.totalMon = parseFloat(totalMoney).toFixed(2);
                }
              });
              //点击移合台弹框确定
              $("#YHTModal .btnSure").off("click").on("click", function () {
                // console.log(curTable);
                // console.log(YHTable);
                var pay_info1 = [];
                var pay_info2 = [];
                curTable.cart.forEach(function (v, i) {
                  var obj = {
                    p_id: v.p_id,
                    p_num: v.num,
                    user_id: v.user_id,
                    name: v.name,
                    price: v.price,
                    cuisine: v.tastes,
                    uom: v.uom,
                  };
                  // if (v.ZFname) {
                  //   obj.cuisine = [{
                  //     id: v.ZFid,
                  //     name: v.ZFname,
                  //     price: v.ZFprice
                  //   }];
                  // } else {
                  //   obj.cuisine = [];
                  // }
                  if (v.combo_details && v.combo_details.length > 0) {
                    obj.combo_details = v.combo_details1;
                  } else {
                    obj.combo_details = [];
                  }

                  pay_info1.unshift(obj);
                });
                YHTable.pay_info.forEach(function (a, b) {
                  var obj = {
                    p_id: a.p_id,
                    p_num: a.num,
                    user_id: a.user_id,
                    name: a.name,
                    price: a.price,
                    cuisine: a.tastes,
                    uom: a.uom
                  };
                  // if (a.ZFname) {
                  //   obj.cuisine = [{
                  //     id: a.ZFid,
                  //     name: a.ZFname,
                  //     price: a.ZFprice
                  //   }];
                  // } else {
                  //   obj.cuisine = [];
                  // }
                  if (a.combo_details && a.combo_details.length > 0) {
                    obj.combo_details = a.combo_details1;
                  } else {
                    obj.combo_details = [];
                  }
                  pay_info2.unshift(obj);
                })
                var pos_order = [{
                    'order_id': curTable.order_id,
                    'origin_reference': curTable.origin_reference,
                    'pay_method': [],
                    'pay_info': pay_info1,
                    'p_total': curTable.totalInfo.totalMon,
                    'floor_id': curTable.floor_id,
                    'table_id': curTable.table_id,
                    "table_name": curTable.name,
                    'exit_order': 1 // 被移动的源单
                  },
                  {
                    'order_id': YHTable.order_id ? YHTable.order_id : App.guid(),
                    'origin_reference': '',
                    'pay_method': [],
                    'pay_info': pay_info2,
                    'p_total': YHTable.p_total,
                    'floor_id': YHTable.tabinfos.floor_id,
                    'table_id': YHTable.tabinfos.table_id,
                    "table_name": YHTable.tabinfos.name,
                    'date_order': App.formatDateTime(new Date()),
                    'new_order': YHTable.tabinfos.state == 3 ? 2 : 1,  // 1是新建订单，2是被移动的源单
                  }
                ]
                App.ajax("ss_wx/change_table_orders", "post", {
                  "pay_flag": 0,
                  "pos_order": JSON.stringify(pos_order)
                }, function (res) {
                  if (res.flag == true) {
                    localStorage.removeItem("tableInfos");
                    localStorage.removeItem("curTable");
                    $("#YHTModal").modal("hide");
                    location.reload();
                    window.samfeng.ChangeTable(JSON.stringify(pos_order));
                  } else {
                    App.msg(res.Message);
                  }
                  if (!res.TokenTrue) {
                    App.msg("Token失效，即将跳转到登录页面", function () {
                      setTimeout(function () {
                        location.href = "../login.html";
                      }, 1000)
                    })
                  }
                })
              })
              // }
              // })
            } else {
              App.msg(res_1.msg, function () {
                setTimeout(function () {
                  location.reload();
                  localStorage.removeItem("tableInfos");
                }, 1000)
              })
            }
          } else {
            App.msg("当前桌台已锁定，请稍后操作", function () {

            });
          }
        } else {
          localStorage.removeItem("curTable");
          App.msg(res1.msg ? res1.msg : "桌台状态已改变，即将刷新桌台状态。", function () {
            setTimeout(function () {
              location.reload();
              localStorage.removeItem("tableInfos");
            }, 1000)
          })
        }
        if (!res_1.TokenTrue) {
          App.msg("Token失效，即将跳转到登录页面", function () {
            setTimeout(function () {
              location.href = "../login.html";
            }, 1000)
          })
        }
      }, "", true)

    })
    $("#YHTModal").on("hidden.bs.modal", function (e) {
      var curTable = JSON.parse(localStorage.getItem("curTable"));
      if (curTable) {
        App.ajax("ss_wx/unlock_table", "post", {
          "table_id": curTable.table_id
        }, function (res5) {
          if (res5.flag == true) {} else {
            App.msg(res5.msg);
          }
          if (!res5.TokenTrue) {
            App.msg("Token失效，即将跳转到登录页面", function () {
              setTimeout(function () {
                location.href = "../login.html";
              }, 1000)
            })
          }
        }, "", true)
      }

    })

  }


  //点击转菜
  function turnDish() {
    $(".turnDish").off("click").on("click", function () {

    })
  }


  //点击打印
  function print(table_info) {
    $(".print").off("click").on("click", function () {
      App.ajax("ss_wx/open_table", "post", {
        table_id: table_info.table_id,
        table_state: 3,
        order_id: table_info.order_id,
        order_timestamp: table_info.order_timestamp,
      }, function (res_1) {
        if (res_1.flag == true) {
          if ($(".btn-jiesuo").hasClass("hidden")) {
            res_1.lock_state = false;
          }
          if (res_1.lock_state == false) {
            if (res_1.update_timestamp == false) {
              var curTable = JSON.parse(localStorage.getItem("curTable"));
              App.ajax("ss_wx/order_print", 'post', {
                order_id: curTable.order_id
              }, function (res) {
                if (res.flag == true) {
                  App.msg("打印中...");
                  window.samfeng.nomalPrint(JSON.stringify(res.pos_order));
                  location.reload();
                } else {
                  App.msg(res.msg);
                }
                if (!res.TokenTrue) {
                  App.msg("Token失效，即将跳转到登录页面", function () {
                    setTimeout(function () {
                      location.href = "../login.html";
                    }, 1000)
                  })
                }
              })
            } else {
              App.msg(res_1.msg, function () {
                setTimeout(function () {
                  location.reload();
                  localStorage.removeItem("tableInfos");
                }, 1000)
              })
            }

          } else {
            App.msg("当前桌台已锁定，请稍后操作", function () {
              setTimeout(function () {
                location.reload();
              }, 1000)
            });

          }
        } else {
          localStorage.removeItem("curTable");
          App.msg(res1.msg ? res1.msg : "桌台状态已改变，即将刷新桌台状态。", function () {
            setTimeout(function () {
              location.reload();
              localStorage.removeItem("tableInfos");
            }, 1000)
          })
        }
        if (!res_1.TokenTrue) {
          App.msg("Token失效，即将跳转到登录页面", function () {
            setTimeout(function () {
              location.href = "../login.html";
            }, 1000)
          })
        }
      })

    })
  }


  //点击催菜
  function UrgeDish() {
    $(".UrgeDish").off("click").on("click", function () {

    })
  }


  //退菜
  function Retire(tableInfos3, table_info, initData) {
    $(".Retire").off("click").on("click", function () {
      retire = true;
      App.ajax("ss_wx/open_table", "post", {
        table_id: table_info.table_id,
        table_state: 3,
        order_id: table_info.order_id,
        order_timestamp: table_info.order_timestamp,
      }, function (res_1) {
        if (res_1.flag == true) {
          if ($(".btn-jiesuo").hasClass("hidden")) {
            res_1.lock_state = false;
          }
          if (res_1.lock_state == false) {
            if (res_1.update_timestamp == false) {
              App.ajax("ss_wx/lock_table", "post", {
                "table_id": initData.table_id
              }, function (res5) {
                if (res5.flag == true) {
                  $(".XDBtns .btn").attr("disabled", "disabled");
                  $(".Table .dishList_YD").prepend("<li class='addLine'><button class='btn QX'>取消</button><button class='btn btn-info QD'>确定</button></li>");
                  $(".Table .main-right .dishList_YD li").eq(1).css("margin-top", "56px");
                  $(".Table .dishList_YD li").each(function (j, k) {
                    var Num = $(this).find(".dishNum").html() * 1;
                    if (Num > 0) {
                      $(this).find(".iconfont").removeClass("hidden");
                    }
                  });
                  $(".Table .dishList_YD .control  .icon-plus-circle").css("color", "#eee");
                  //点击购物车加减改变数量
                  var cart1 = JSON.parse(localStorage.getItem("curTable") || "{}").cart;
                  $(".Table .dishList_YD .control").on("click", ".iconfont", function () {
                    var control = $(this).data("control");
                    var dishNum = "";
                    var dishPrice = $(this).parents("li").data("tcinfos").price;
                    var ind = $(this).parents("li").index() - 1;
                    if (ind <= 0) {
                      ind = 0;
                    }
                    // cart1[ind].refundNum = 0;
                    var dishName = $(this).parents("li").find(".dishName").html();
                    var c_id = $(this).parents("li").data("tcinfos").cid;
                    var totalCount = $(".Table .money .dishToal").html() * 1;
                    var totalMon = $(".Table .money .dishMoney").html() * 1;
                    if (control == "minus") {
                      // 点击减号
                      dishNum = +$(this).next().html();
                      dishNum -= 1;
                      if (dishNum <= 0) {
                        dishNum = 0;
                        $(this).parents("li").css("color", "red");
                        $(this).parents("li").find(".iconfont").addClass("hidden");
                      }
                      $(this).next().html(dishNum);
                      $(this).parents("li").find(".dishPrice").html((dishPrice * dishNum).toFixed(2));
                      $(".Table .money .dishToal").html(totalCount - 1 <= 0 ? '0.0' : totalCount - 1);
                      $(".Table .money .dishMoney").html(totalMon - dishPrice <= 0 ? '0.0' : (totalMon - dishPrice).toFixed(2));

                      cart1[ind].num -= 1;
                      if (!cart1[ind].hasOwnProperty("refundNum")) {
                        cart1[ind].refundNum = 1;
                      } else {
                        cart1[ind].refundNum += 1;
                      }
                      if (cart1[ind].num <= 0) {
                        cart1[ind].num = 0;
                      };
                    }
                  });

                  //点击退菜的取消
                  $(".QX").on("click", function () {
                    $(".Table .dishList_YD .control .iconfont").addClass("hidden");
                    $(this).parents("li").remove();
                    $(".Table .main-right .dishList_YD li").eq(0).css("margin-top", "0");
                    $(".XDBtns .btn").removeAttr("disabled", "disabled");
                    var orderHtml3 = "";
                    $.each(initData.cart, function (i, v) {
                      orderHtml3 += "<li class='" + (v.num == 0 ? "red" : "") + "' data-TCInfos='" + JSON.stringify(v) + "'><div class='pack hidden'>打包</div><div class='waitCall hidden'>等叫</div><div class='dishName'>" + v.name + "</div><div class='control'> <i data-control='minus' class='iconfont icon-minus-circle hidden'></i><span class='dishNum'>" + v.num + "</span> <i data-control='plus' class='iconfont icon-plus-circle hidden'></i> </div><div class='dishPrice'>" + (v.price * v.num).toFixed(2) + "</div></li>"
                    });
                    $(".Table .dishList_YD").html(orderHtml3);
                    $(".Table .main-right .dishToal").html(initData.totalInfo.totalCount);
                    $(".Table .main-right .dishMoney").html(initData.totalInfo.totalMon.toFixed(2));
                    App.ajax("ss_wx/unlock_table", "post", {
                      "table_id": initData.table_id
                    }, function (res5) {
                      retire = false;
                      if (res5.flag == true) {} else {
                        App.msg(res5.msg);
                      }
                      if (!res5.TokenTrue) {
                        App.msg("Token失效，即将跳转到登录页面", function () {
                          setTimeout(function () {
                            location.href = "../login.html";
                          }, 1000)
                        })
                      }
                    })
                  });

                  //点击退菜的确定
                  $(".QD").on("click", function () {
                    var curTable = JSON.parse(localStorage.getItem("curTable") || "{}");
                    curTable.cart = cart1;
                    if (cart1.length <= 1) {
                      let pnum = 0;
                      cart1.forEach(function (k, p) {
                        pnum = k.num;
                      });
                      if (pnum < 1) {
                        App.msg("当前退菜的桌台只有一道菜,可以考虑直接点击'退单'!");
                        return
                      }
                    }
                    curTable.totalInfo = {
                      totalCount: $(".Table .money .dishToal").html(),
                      totalMon: $(".Table .money .dishMoney").html()
                    }
                    localStorage.setItem("curTable", JSON.stringify(curTable));
                    total();
                    retire = false;
                    App.uploadCurTable(curTable, true);
                    $(".XDBtns .btn").removeAttr("disabled", "disabled");
                  })
                } else {
                  App.msg("锁定桌台失败");
                }
                if (!res5.TokenTrue) {
                  App.msg("Token失效，即将跳转到登录页面", function () {
                    setTimeout(function () {
                      location.href = "../login.html";
                    }, 1000)
                  })
                }
              })
            } else {
              App.msg(res_1.msg, function () {
                setTimeout(function () {
                  location.reload();
                  localStorage.removeItem("tableInfos");
                }, 1000)
              });
            }

          } else {
            App.msg("当前桌台已锁定，请稍后操作", function () {
              setTimeout(function () {
                location.reload();
              }, 1000)
            });
          }
        } else {
          localStorage.removeItem("curTable");
          App.msg(res1.msg ? res1.msg : "桌台状态已改变，即将刷新桌台状态。", function () {
            setTimeout(function () {
              location.reload();
              localStorage.removeItem("tableInfos");
            }, 1000)
          })
        }
        if (!res_1.TokenTrue) {
          App.msg("Token失效，即将跳转到登录页面", function () {
            setTimeout(function () {
              location.href = "../login.html";
            }, 1000)
          })
        }
      })

    });

  }
  //退单
  function TuiDan(table_info) {
    $(".TuiDan").off("click").on("click", function () {
      App.ajax("ss_wx/open_table", "post", {
        table_id: table_info.table_id,
        table_state: 3,
        order_id: table_info.order_id,
        order_timestamp: table_info.order_timestamp,
      }, function (res_1) {
        if (res_1.flag == true) {
          if ($(".btn-jiesuo").hasClass("hidden")) {
            res_1.lock_state = false;
          }
          if (res_1.lock_state == false) {
            if (res_1.update_timestamp == false) {
              var curTable = JSON.parse(localStorage.getItem("curTable"));
              curTable.origin_reference = res_1.origin_reference;
              curTable.table_name = table_info.name;
              var tableInfos = JSON.parse(localStorage.getItem("tableInfos"));
              App.ajax("ss_wx/lock_table", "post", {
                "table_id": curTable.table_id
              }, function (res) {
                if (res.flag == true) {
                  App.confirm("退单后该桌台将恢复空闲", function () {
                    App.ajax("ss_wx/hzg_refund_orders", "post", {
                      refund_order_id: curTable.order_id
                    }, function (resTui) {
                      if (resTui.flag == true) {
                        tableInfos.forEach(function (v, i) {
                          if (v.table_id == curTable.table_id) {
                            tableInfos.splice(i, 1);
                          }
                        })
                        localStorage.setItem("tableInfos", JSON.stringify(tableInfos));
                        localStorage.removeItem("curTable");
                        App.msg("退单成功", function () {
                          setTimeout(function () {
                            location.reload();
                          }, 1000)
                        });
                        curTable.cart.forEach(function (v, i) {
                          v.p_num = v.num;
                          v.combo_details = v.combo_details !== "" ? v.combo_details:[];
                          v.combo_details1 = false;
                        });
                        window.samfeng.AllRefundOrder(JSON.stringify(curTable));
                      } else {
                        App.msg(resTui.msg);
                      }
                      if (!resTui.TokenTrue) {
                        App.msg("Token失效，即将跳转到登录页面", function () {
                          setTimeout(function () {
                            location.href = "../login.html";
                          }, 1000)
                        })
                      }
                    })
                  }, function () {
                    App.ajax("ss_wx/unlock_table", "post", {
                      "table_id": curTable.table_id
                    }, function (res5) {
                      if (res5.flag == true) {} else {
                        App.msg(res5.msg);
                      }
                      if (!res5.TokenTrue) {
                        App.msg("Token失效，即将跳转到登录页面", function () {
                          setTimeout(function () {
                            location.href = "../login.html";
                          }, 1000)
                        })
                      }
                    })
                  })
                } else {
                  App.msg(res.msg);
                }
                if (!res_1.TokenTrue) {
                  App.msg("Token失效，即将跳转到登录页面", function () {
                    setTimeout(function () {
                      location.href = "../login.html";
                    }, 1000)
                  })
                }
              })
            } else {
              App.msg(res_1.msg);
            }
            if (!res.TokenTrue) {
              App.msg("Token失效，即将跳转到登录页面", function () {
                setTimeout(function () {
                  location.href = "../login.html";
                }, 1000)
              })
            }

          } else {
            App.msg("当前桌台已锁定，请稍后操作", function () {
              setTimeout(function () {
                location.reload();
              }, 1000)
            });
          }
        } else {
          localStorage.removeItem("curTable");
          App.msg(res1.msg ? res1.msg : "桌台状态已改变，即将刷新桌台状态。", function () {
            setTimeout(function () {
              location.reload();
              localStorage.removeItem("tableInfos");
            }, 1000)
          })
        }
        if (!res_1.TokenTrue) {
          App.msg("Token失效，即将跳转到登录页面", function () {
            setTimeout(function () {
              location.href = "../login.html";
            }, 1000)
          })
        }
      })

    })
  }
  //获取总数量、总金额
  function total() {
    var totalInfo = {};
    totalInfo.totalCount = $(".Table .money .dishToal").html();
    totalInfo.totalMon = $(".Table .money .dishMoney").html();
    var curTable = JSON.parse(localStorage.getItem("curTable") || "{}");
    curTable.totalInfo = totalInfo;
    curTable.p_total = totalInfo.totalMon;
    localStorage.setItem("curTable", JSON.stringify(curTable));
    var tableInfos = JSON.parse(localStorage.getItem("tableInfos") || "[]");
    if (tableInfos && tableInfos.length > 0) {
      $.each(tableInfos, function (j, k) {
        if (k.floor_id == curTable.floor_id && k.table_id == curTable.table_id) {
          // tableInfos[j] = curTable;
          tableInfos.splice(j, 1, curTable);
        }
      })
      localStorage.setItem("tableInfos", JSON.stringify(tableInfos));
    }
  }


  //点击加菜
  $(".addDish").off("click").on("click", function () {
    var curTable = JSON.parse(localStorage.getItem("curTable"));

    App.ajax("ss_wx/open_table", "post", {
      table_id: curTable.table_id,
      table_state: 3,
      order_id: curTable.order_id,
      order_timestamp: curTable.order_timestamp,
    }, function (res_1) {
      if (res_1.flag == true) {
        if ($(".btn-jiesuo").hasClass("hidden")) {
          res_1.lock_state = false;
        }
        if (res_1.lock_state == false) {
          if (res_1.update_timestamp == false) {
            App.ajax("ss_wx/lock_table", "post", {
              "table_id": curTable.table_id
            }, function (res5) {
              if (res5.flag == true) {
                location.href = "Order.html?addOrder=true";
              } else {
                App.msg("锁定桌台失败");
              }
              if (!res5.TokenTrue) {
                App.msg("Token失效，即将跳转到登录页面", function () {
                  setTimeout(function () {
                    location.href = "../login.html";
                  }, 1000)
                })
              }
            })
          } else {
            App.msg(res_1.msg, function () {
              setTimeout(function () {
                location.reload();
                localStorage.removeItem("tableInfos");
              }, 1000)
            });
          }

        } else {
          App.msg("当前桌台已锁定，请稍后操作", function () {
            setTimeout(function () {
              location.reload();
            }, 1000)
          });
        }
      } else {
        localStorage.removeItem("curTable");
        App.msg(res_1.msg ? res_1.msg : "桌台状态已改变，即将刷新桌台状态。", function () {
          setTimeout(function () {
            location.reload();
            localStorage.removeItem("tableInfos");
          }, 1000)
        })
      }
      if (!res_1.TokenTrue) {
        App.msg("Token失效，即将跳转到登录页面", function () {
          setTimeout(function () {
            location.href = "../login.html";
          }, 1000)
        })
      }
    })

  })
  //点击解锁
  $(".btn-jiesuo").off("click").on("click", function () {
    // $("#JSModal .modal-title").html("解锁");
    // $("#JSModal .modal-body label").html("解锁密码：");
    // $("#JieSuo").val("");
    $("#JSModal").modal("show");
    // lock = true;
  });

  $('#JSModal').on('shown.bs.modal', function () {
    $('#JieSuo').focus()
  })

  $("#JSModal .btnSure").off("click").on("click", function () {
    let table_name = $('#JieSuo').val();
    let table_id = false;
    let tableinfos = JSON.parse(localStorage.getItem('tableInfos'));
    if (tableinfos) {
      tableinfos.forEach(function (x, y) {
        if (x.name == table_name) {
          table_id = x.table_id;
        }
      });
      if (table_id) {
        App.unlocktable(table_id);
        $("#JSModal").modal("hide");
      } else {
        App.unlocktable1(table_name);
      }
    } else {
      App.unlocktable1(table_name);
    }

  });

  //点击交班
  $(".jiaoban").off("click").on("click", function () {
    if (retire) {
      App.msg('当前正在进行退菜操作,点击"取消"或者"确定"按钮');
    } else {
      App.confirm("确定交班？", function () {
        App.ajax("ss_wx/close_store", "post", {}, function (res) {
          if (res.flag == true) {
            App.msg("交班成功", function () {
              setTimeout(function () {
                location.href = "../login.html";
              }, 1000)
            })
            localStorage.removeItem("curTable");
            localStorage.removeItem("tableInfos");
            localStorage.removeItem("allTables");
            localStorage.removeItem("payCode");
            localStorage.removeItem("category_product");
          } else {
            App.msg(res.msg)
          }
          if (!res.TokenTrue) {
            App.msg("Token失效，即将跳转到登录页面", function () {
              setTimeout(function () {
                location.href = "../login.html";
              }, 1000)
            })
          }
        })
      }, function () {

      })
    }
  })

})